using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using TXLooker.WorkItemExtention.Properties;

namespace TXLooker.WorkItemExtention.Core
{
    internal class WIQueriesGroup : WIObject
    {
        public QueryScope QueryScope
        {
            get { return this.m_scope; }
            private set
            {
                this.m_scope = value;
            }
        }
        private QueryScope m_scope = QueryScope.Private;

        public WIQueriesGroup( WIQueriesRoot parent, QueryScope scope ) : base(parent)
        {
            this.QueryScope = scope;
            this.InitFolder( this.Parent.Folder );
        }

        private WIQueriesRoot QueriesRoot
        {
            get { return ( this.Parent as WIQueriesRoot ); }
        }
        public WIProjectProvider ProjectProvider
        {
            get { return this.QueriesRoot.ProjectProvider; }
        }
        public Uri ProjectUri
        {
            get { return this.QueriesRoot.ProjectUri; }
        }
        public string ProjectName
        {
            get { return this.QueriesRoot.ProjectName; }
        }
        public Project GetProject()
        {
            return this.QueriesRoot.GetProject();
        }

        public override WIObjectType Type
        {
            get { return WIObjectType.QueriesGroup; }
        }
        public override string FolderName
        {
            get 
            {
                switch( this.QueryScope )
                {
                    case QueryScope.Private:
                        return Resources.CUSTOM_QUERIES_FOLDER_NAME;
                    case QueryScope.Public:
                        return Resources.TEAM_QUERIES_FOLDER_NAME;
                    default:
                        throw new NotImplementedException();
                }
            }
        }

        protected override void UpdateData()
        {
            //1. try to get stored queries list from server
            StoredQueryCollection storedQueries = null;

            try 
            { 
                Project project = this.GetProject(); 
                storedQueries = project.StoredQueries;
                storedQueries.Refresh();
            }
            catch( System.Exception ex ) 
            {
                Debug.WriteLine( ex.ToString() ); 
            }

            if( storedQueries == null ) return;


            //2. get current state
            Dictionary<Guid, WIObject> prevQueries = new Dictionary<Guid, WIObject>();

            lock( this.Childs )
            {
                foreach( object key in this.Childs.Keys )
                    prevQueries.Add( (Guid)key, this.Childs[key] );
            }

            //3. Find new & deleted queries
            List<WIObject> newQueries = new List<WIObject>();

            foreach( StoredQuery item in storedQueries )
            {
                if( item.QueryScope != this.QueryScope )
                    continue;

                if( prevQueries.ContainsKey(item.QueryGuid) )
                {
                    prevQueries.Remove(item.QueryGuid);
                }
                else
                {
                    newQueries.Add( new WIStoredQuery(this, item) );
                }
            }

            //remove queries
            if( prevQueries.Values.Count != 0 )
            {
                List<WIObject> removeQueries = new List<WIObject>( prevQueries.Values );

                this.RaisePreRemoveChildItems( removeQueries );

                lock( this.Childs )
                {
                    foreach( WIObject item in removeQueries )
                    {
                        try    
                        { 
                            item.Delete(); 
                        }
                        catch( System.Exception ex )
                        {
                            Debug.WriteLine( ex.ToString() );
                        }

                        this.Childs.Remove( (item as WIStoredQuery).QueryGuid );
                    }
                }
            }

            if( newQueries.Count != 0 )
            {
                lock( this.Childs )
                {
                    foreach( WIStoredQuery item in newQueries )
                        this.Childs.Add(item.QueryGuid, item);
                }

                this.RaiseNewChildItems(newQueries);
            }
        }

        public override OlFolderView FolderView
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
        
    }
}
